﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Business;
using MyWeb.Data;
namespace MyWeb.Admins
{
    public partial class tb_User : System.Web.UI.Page
    {
        static bool Insert = false; 
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) { 
                Session["folder"] = "tb_User"; 
                BindGrid(); 
            }
        }
        private void BindGrid() { 
            grdtb_User.DataSource = tb_UserService.tb_User_GetByAll(); 
            grdtb_User.DataBind();
        }

        protected void btnThem_Click(object sender, EventArgs e) { 
            pnShow.Visible = false; pnUpdate.Visible = true; 
            ControlClass.ResetControlValues(this); 
            Session["upload"] = null; Insert = true;
        }
        protected void btnXoa_Click(object sender, EventArgs e) { 
            try { 
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdtb_User.Items.Count; i++) {
                    item = grdtb_User.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item) {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked) {
                            string strId = item.Cells[1].Text; 
                            try 
                            { 
                                tb_UserService.tb_User_Delete(strId);
                            }
                            catch {
                            } 
                        } 
                    } 
                } 
                grdtb_User.CurrentPageIndex = 0;
                BindGrid();
            } catch {
            }
        }
        protected void grdtb_User_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Data.tb_UserInfo strCA = new tb_UserInfo();
            strCA.Username = e.CommandArgument.ToString();
            if (e.CommandName == "Edit") {
                DataTable dt = tb_UserService.tb_User_GetById(strCA);
                txtUsername.Text = dt.Rows[0]["Username"].ToString();
                txtPassword.Text = dt.Rows[0]["Password"].ToString();
                txtAddress.Text = dt.Rows[0]["Address"].ToString();
                txtPhone.Text = dt.Rows[0]["Phone"].ToString();
                if (dt.Rows[0]["Gender"].ToString() == "True")
                    ckbGender.Checked = true;
                else
                    ckbGender.Checked = false;
                if (dt.Rows[0]["Status"].ToString() == "True")
                    ckbStatus.Checked = true;
                else
                    ckbStatus.Checked = false;
                pnUpdate.Visible = true;
                pnShow.Visible = false; 
            } else 
            { 
                tb_UserService.tb_User_Delete(strCA.Username); BindGrid();
            }
        }
        
        protected void lblBack_Click(object sender, EventArgs e) {
            pnUpdate.Visible = false;
            pnShow.Visible = true; 
            Insert = false; }
        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();
            #region[TestInput]
            if (txtUsername.Text.Trim().Equals("")) {
                WebMsgBox.Show("Username không thể trống !"); 
                txtUsername.Focus();
                return; 
            }
            if (txtPassword.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Password không thể trống !");
                txtPassword.Focus(); 
                return; 
            } 
            if(txtAddress.Text.Trim().Equals("")) {
                WebMsgBox.Show("Address không thể trống !");
                txtAddress.Focus(); return;
            }
            if (txtPhone.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Phone không thể trống !"); 
                txtPhone.Focus(); return; 
            } 
            
            #endregion
            String status;
            if (ckbStatus.Checked)
                status = "True";
            else
                status = "False";
            String gender;
            if (ckbGender.Checked)
                gender = "True";
            else
                gender = "False";
            Data.tb_UserInfo obj = new Data.tb_UserInfo(); 
            obj.Username = txtUsername.Text; 
            obj.Password = txtPassword.Text;
            obj.Address = txtAddress.Text;
            obj.Phone = txtPhone.Text;
            obj.Gender = gender;
            obj.Status = status; 
            if (Insert == true) {
                tb_UserService.tb_User_Insert(obj); 
            } else { 
                tb_UserService.tb_User_Update(obj); 
            } 
            BindGrid(); 
            pnShow.Visible = true; 
            pnUpdate.Visible = false;
            Insert = false;
        }
        
        protected void grdtb_User_PageIndexChanged(object source, DataGridPageChangedEventArgs e) {
            grdtb_User.CurrentPageIndex = e.NewPageIndex; 
            BindGrid(); 
        }
        protected void grdtb_User_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType; if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll"); if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdtb_User.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")"); ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
                    }
                }
            }
        }
    }
}